package com.shop.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import com.shop.entity.Address;
import com.shop.mapper.AddressMapper;
import com.shop.model.vo.Rsp;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * (Address)表服务接口
 *
 * @author makejava
 * @since 2020-11-21 12:08:45
 */
@Service
@AllArgsConstructor
public class AddressService {
    private final AddressMapper addressMapper;

    public Rsp getAddressById(Integer id) {
        return Rsp.success(addressMapper.selectOne(new QueryWrapper<Address>()
                .lambda().eq(Address::getUserId, id)
                .eq(Address::getIsDefault, 1)));
    }

    public Rsp getAllAddressById(Integer id) {
        return Rsp.success(addressMapper.selectList(new QueryWrapper<Address>()
                .lambda().eq(Address::getUserId, id)));
    }

    public Rsp addAddress(Address address) {
        if (StringUtils.isEmpty(address.getId()) || address.getId()==0) {
            addressMapper.insert(address);
        } else {
            addressMapper.updateById(address);
        }
        return Rsp.success(true);
    }

    public Rsp setDefaultAddress(Integer userId, Integer addrId) {
        addressMapper.setDefaultAddress(userId, addrId);
        return Rsp.success(true);
    }

    public Rsp getAddress(Integer userId, Integer addrId) {
        return Rsp.success(addressMapper.selectOne(new QueryWrapper<Address>()
                .lambda().eq(Address::getUserId, userId)
                .eq(Address::getId, addrId)));
    }

    public Rsp delAddress(Integer addrId) {
        addressMapper.deleteById(addrId);
        return Rsp.success(true);
    }
}
